var uTree = {
	selectedBackgroundColor : 'lightgreen',
	selectedColor : 'black',
	imgClosed : 'url(images/closed.png) no-repeat left top',
	imgOpen : 'url(images/open.png) no-repeat left top',
	selected : new Array(),
	noBubble : function(e){ (typeof e!="undefined") ? e.stopPropagation() : event.cancelBubble=true; },
	init : function(t, options) {
		tt = $(t);
		if (!tt) return null;
		tt.className='utree';
		tt.getElements('ul').each( function(ulel) {
			uTree.build(tt, ulel);
			});
		uTree.open(tt.getElement('ul'));
		return tt;
	},
	build : function (t, ulel) {
		ulel.parentNode.className='submenu';

		if ( ! ulel.get('rel') )		uTree.close(ulel);
		else if (ulel.get('rel')=='open')	uTree.expand(t, ulel);

		$(ulel.parentNode).addEvent('click', function(e){
			if (e.target.id) {
				uTree.select(e.target);
				return uTree.noBubble(e);
				}
			var submenu=this.getElement('ul');
			if (submenu.get('rel') == 'closed')	uTree.open(submenu);
			else					uTree.close(submenu);
			uTree.noBubble(e)
			});
		ulel.addEvent('click', function(e){
			if (e.target.id) {
				uTree.select(e.target);
				return uTree.noBubble(e);
				}
			uTree.noBubble(e)
			});
	},
	select : function (uspan) {
		uspan.style.backgroundColor = uTree.selectedBackgroundColor;
		uspan.style.color = uTree.selectedColor;
		if (uTree.selected[uspan.className]) {
			uTree.selected[uspan.className].style.backgroundColor = '';
			uTree.selected[uspan.className].style.color = '';
			}
		uTree.selected[uspan.className] = uspan;
		$(uspan.className).value = uspan.id;
		return uspan;
	},
	open : function (ulel) {
		if (ulel) {
			ulel.show();
			ulel.set('rel', 'open');
			ulel.parentNode.style.background = uTree.imgOpen;
			}
		return ulel;
	},
	close : function (ulel) {
		if (ulel) {
			ulel.hide();
			ulel.setAttribute('rel', 'closed')
			ulel.parentNode.style.background = uTree.imgClosed;
			}
		return ulel;
	},
	expand : function(rootnode, currentnode){
		while (currentnode!=rootnode){
			if (currentnode.tagName=='ul')
				uTree.open(currentnode);
			currentnode=currentnode.parentNode
		}
		return rootnode;
	},
	flatten : function(t, action){
		t.getElements('ul').each( function (ulel) {
			if (action=='expand') 	uTree.open (ulel);
			else			uTree.close (ulel);
		});
		return t;
	}
}
